package com.sebiotec.glp.limoapp.service;

import android.app.IntentService;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;


import androidx.core.app.NotificationCompat;

import com.allen.library.interceptor.Transformer;
import com.allen.library.observer.CommonObserver;
import com.bill.module_notification.ChannelEntity;
import com.bill.module_notification.ImportanceType;
import com.bill.module_notification.NotifyManager;
import com.sebiotec.basecommon.utils.constant.SjmConstant;
import com.sebiotec.basecommon.utils.language.LocalManageUtil;
import com.sebiotec.basecommon.utils.tools.SPUtils;
import com.sebiotec.glp.limoapp.APP;
import com.sebiotec.glp.limoapp.AmbassadorMainActivity;
import com.sebiotec.glp.limoapp.JobMainActivity;
import com.sebiotec.glp.limoapp.MainActivity;
import com.sebiotec.glp.limoapp.ManagerMainActivity;
import com.sebiotec.glp.limoapp.model.NoticeMsg;
import com.sebiotec.glp.limoapp.model.PostNoticeMsg;
import com.sebiotec.glp.limoapp.net.MainApiHelper;


import java.util.HashMap;


public class RepetitionService extends IntentService {


    private NotifyManager notifyManager;
    private PendingIntent mainPendingIntent;

    public RepetitionService() {
        super("RepetitionService");
    }


    @Override
    public void onCreate() {
        super.onCreate();
        initNocation();
        Log.d("RepetitionService", "onCreate");
    }
    private void initNocation() {
        notifyManager = new NotifyManager(this);

        ChannelEntity chatChannel = new ChannelEntity(Constants.CHANNEL_CHAT, "新聊天消息", ImportanceType.IMPORTANCE_HIGH);
        chatChannel.setDescription("个人或群组发来的聊天消息");
        notifyManager.createNotificationGroupWithChannel(Constants.GROUP_CHAT, "聊天消息", chatChannel);

    }
    @Override
    protected void onHandleIntent(Intent intent) {
        int id=0;
        if (1==SPUtils.get(SjmConstant.ROLEID, 0)){
             //book
           id=SPUtils.get(SjmConstant.BOOK,0);
        }else if (3==SPUtils.get(SjmConstant.ROLEID, 0)){
            //driver
            id=SPUtils.get(SjmConstant.DRIVER,0);
        }else if (5==SPUtils.get(SjmConstant.ROLEID, 0)){
           //manager
            id=SPUtils.get(SjmConstant.MANAGER,0);
        }else if (2==SPUtils.get(SjmConstant.ROLEID, 0)){
            //manager
            id=SPUtils.get(SjmConstant.Ambass,0);
        }


         //请求网络
        HashMap<String,Object> map=new HashMap<String, Object>();
        map.put("LoginId", SPUtils.get(SjmConstant.LOGINID,""));
        map.put("Token", SPUtils.get(SjmConstant.TOKEN,""));
        map.put("Lang", LocalManageUtil.getSMJSetLanguageLocales(APP.context));
      PostNoticeMsg msg=new PostNoticeMsg();
        msg.setId(SPUtils.get(SjmConstant.NOID,0));
        if (!TextUtils.isEmpty(SPUtils.get(SjmConstant.LOGINID,""))){
         MainApiHelper.geRadomApi()
                    .getMessage(map,id)
                    .compose(Transformer.<NoticeMsg>switchSchedulers())
                    .subscribe(new CommonObserver<NoticeMsg>() {
                        @Override
                        protected void onError(String errorMsg) {
                            TimersUtils.setKeepAliveAlarm(getApplicationContext(),true);
                        }

                        @Override
                        protected void onSuccess(NoticeMsg s) {
                            if (1==SPUtils.get(SjmConstant.ROLEID, 0)){
                                //book
                                SPUtils.put(SjmConstant.BOOK,s.Id);
                            }else if (3==SPUtils.get(SjmConstant.ROLEID, 0)){
                                //driver
                                SPUtils.put(SjmConstant.DRIVER,s.Id);
                            }else if (5==SPUtils.get(SjmConstant.ROLEID, 0)){
                                //manager
                                SPUtils.put(SjmConstant.MANAGER,s.Id);
                            }else if (2==SPUtils.get(SjmConstant.ROLEID, 0)){
                                //manager
                                SPUtils.put(SjmConstant.Ambass,s.Id);
                            }
                            handleChat(s);
                            TimersUtils.setKeepAliveAlarm(getApplicationContext(),true);
                        }
                    });


        }






    }




    public void handleChat(NoticeMsg s) {
        if (!notifyManager.areNotificationsEnabled()) {
            Toast.makeText(this, "总通知被关闭", Toast.LENGTH_SHORT).show();
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (!notifyManager.areChannelsEnabled(Constants.CHANNEL_CHAT)) {
                Toast.makeText(getApplicationContext(), "当前渠道通知被关闭", Toast.LENGTH_SHORT).show();
                return;
            }
        }

        if (1==SPUtils.get(SjmConstant.ROLEID, 0)){
            //book
            Intent mainIntent = new Intent(this, MainActivity.class);

            Bundle bundle = new Bundle();
            bundle.putSerializable("tz", "tz");
            mainIntent.putExtras(bundle);

             mainPendingIntent = PendingIntent.getActivity(this, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        }else if (3==SPUtils.get(SjmConstant.ROLEID, 0)){
            //driver
            Intent mainIntent = new Intent(this, JobMainActivity.class);

            Bundle bundle = new Bundle();
            bundle.putSerializable("tz", "tz");
            mainIntent.putExtras(bundle);

            mainPendingIntent = PendingIntent.getActivity(this, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        }else if (5==SPUtils.get(SjmConstant.ROLEID, 0)){
            //manager
            Intent mainIntent = new Intent(this, ManagerMainActivity.class);

            Bundle bundle = new Bundle();
            bundle.putSerializable("tz", "tz");
            mainIntent.putExtras(bundle);

            mainPendingIntent = PendingIntent.getActivity(this, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        }else if (2==SPUtils.get(SjmConstant.ROLEID, 0)){
            //manager
            Intent mainIntent = new Intent(this, AmbassadorMainActivity.class);

            Bundle bundle = new Bundle();
            bundle.putSerializable("tz", "tz");
            mainIntent.putExtras(bundle);

            mainPendingIntent = PendingIntent.getActivity(this, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        }


        NotificationCompat.Builder builder = notifyManager.getDefaultBuilder(Constants.CHANNEL_CHAT);
        builder.setContentTitle("Received a message from SJM");
        builder.setContentText(s.getBookingId()+"");
        builder.setNumber(1);
        builder.setAutoCancel(true);
        builder.setContentIntent(mainPendingIntent);
        Notification notification = builder.build();
        notifyManager.notifyNotify(notification);
    }


    @Override
    public void onDestroy() {

        super.onDestroy();
        Log.d("RepetitionService", "onDestroy");
    }





}
